﻿using FoosballEye.BLL.Configuration;
using FoosballEye.BLL.Enums;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;

namespace FoosballEye.BLL.Services
{
    using System.Globalization;

    public static class EmailService
    {
        public static void SendEmail(GameFinishedPlayers gamePlayers)
        {
            string header = "<tr><td><b>Original emails:</b></td></tr><tr><td colspan='2'>{0}</td></tr><tr><td colspan='2'>{1}</td></tr><tr><td colspan='2'>{2}</td></tr><tr><td colspan='2'  style ='padding-bottom:10px'>{3}</td></tr>";
            using (SmtpClient smtp = new SmtpClient(ConfigWrapper.SmtpServer))
            {
                string body = FillBody(gamePlayers, "FinishedMatch.htm");

                MailMessage mailMessage = new MailMessage
                {
                    From = new MailAddress(ConfigWrapper.FoosballEyeFromEmail)
                };

                List<string> emailSpoofingRecepients = ConfigWrapper.EmailSpoofingRecepients.Split(';').ToList();
                if (ConfigWrapper.TypeOfSite != SiteType.Prod)
                {                    
                    foreach (string recipient in emailSpoofingRecepients)
                    {
                        mailMessage.To.Add(recipient);
                    }

                    header = string.Format(header, gamePlayers.BlackAttackerEmail, gamePlayers.BlackDefenderEmail, gamePlayers.WhiteAttackerEmail, gamePlayers.WhiteDefenderEmail);
                }
                else
                {
                    mailMessage.To.Add(gamePlayers.BlackAttackerEmail);
                    mailMessage.To.Add(gamePlayers.BlackDefenderEmail);
                    mailMessage.To.Add(gamePlayers.WhiteAttackerEmail);
                    mailMessage.To.Add(gamePlayers.WhiteDefenderEmail);

                    foreach (string recipient in emailSpoofingRecepients)
                    {
                        mailMessage.Bcc.Add(recipient);
                    }

                    header = string.Empty;                    
                }

                body = body.Replace("##Header##", header);
                mailMessage.Subject = (ConfigWrapper.TypeOfSite != SiteType.Prod ? string.Format("({0}) ", ConfigWrapper.TypeOfSite) : string.Empty) + "FoosballEye - new match entered";
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = body;
                smtp.Send(mailMessage);
            }
        }

        private static string FillBody(GameFinishedPlayers gamePlayers, string emailTemplate)
        {
            string body;
            using (StreamReader streamReader = new StreamReader(gamePlayers.EmailTemplatesFolder + emailTemplate))
            {
                body = streamReader.ReadToEnd();
                streamReader.Close();
            }

            body = body
                    .Replace("##Winners##", gamePlayers.Winners)
                    .Replace("##Losers##", gamePlayers.Losers)
                    .Replace("##Result##", gamePlayers.Result)
                    .Replace("##WonPoints##", gamePlayers.WonPoints)
                    .Replace("##TimeOfGame##", gamePlayers.TimeOfGame)
                    .Replace("##SeasonId##", gamePlayers.SesasonId.ToString(CultureInfo.InvariantCulture));

            if (gamePlayers.IsDraw)
            {
                body = body
               .Replace("Winners:", "Players 1:")
               .Replace("Losers:", "Players 2:")
               .Replace("Won Points:", "Splitted Points:");
            }

            return body;
        }
    }
}
